﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class OrderShow : AdminPage
    {
        BLL.CNVP_Order bll = new BLL.CNVP_Order();
        Model.CNVP_Order model = new Model.CNVP_Order();
        BLL.CNVP_Products bll_Products = new BLL.CNVP_Products();
        BLL.CNVP_ProductsDetail bll_ProductsDetail = new BLL.CNVP_ProductsDetail();
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 94)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                DataInfo();
                OrderProListInfo();
            }
        }
        private void DataInfo()
        {
            model = bll.GetSingle_CNVP_OrderById(id);
            if (model != null)
            {
                ViewState["ono"] = model.OrderNum;
                LabTime.Text = model.PostTime.ToString();
                us.Items.Clear();
                OrderStateInfo(model.OrderState);
                LabZT.Text = BLL.BLLOrder.getorderzt(model.OrderState);
                ViewState["OrderState"] = model.OrderState;
                ViewState["BuyerID"] = model.BuyerID;
                Labprice.Text = model.ProductsTotalAmount.ToString();
                txtDeliveryPrice.Text = model.DeliveryPrice.ToString();
                var model_Users=new BLL.CNVP_Users().GetSingle_CNVP_UsersById(model.BuyerID);
                if (model_Users != null)
                {
                    var model_UserLevel = new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(model_Users.UserLevelID);
                    if (model_UserLevel != null)
                    {
                        LabUser.Text = model_UserLevel.LevelName;
                    }
                    LabUser.Text = model_Users.UserName;
                    LabGetJF.Text = model_Users.PointsNum.ToString();
                }
                string Address_Str = new BLL.CNVP_Province().GetSingle_CNVP_ProvinceByCode(model.Province).Name + "" + new BLL.CNVP_City().GetSingle_CNVP_CityByCode(model.City).Name;
                Address_Str += new BLL.CNVP_Area().GetSingle_CNVP_AreaByCode(model.Area).Name + model.GetAddress;
                LaAddress.Text = Address_Str;

                string StandardValue_Str = "";
                var OrderProShopMustInfo_List = new BLL.CNVP_OrderProShopMustInfo().GetAll_CNVP_OrderProShopMustInfo(id);
                foreach (Model.CNVP_OrderProShopMustInfo o in OrderProShopMustInfo_List)
                {
                    if (o.StandardValue != "")
                    {
                        StandardValue_Str += o.StandardValue + "；";
                    }
                }
                LabText.Text = StandardValue_Str;

                Labpriall.Text = model.OrderTotalAmount.ToString();
                LabTel.Text = model.Phone;
                LabMob.Text = model.MobilePhone;
                LabZip.Text = model.ZipCode;
                LabName.Text = model.GetTrueName;
                ViewState["DeliveryWay"] = model.DeliveryWay;
                lbDeliveryWay.Text = model.DeliveryWay;
                lbFHno.Text = model.DeliveryNo;
                lbFHnoCompany.Text = model.DeliveryCompany;
                if (model.OrderState == 0)
                {
                    //发货单号
                    FH.Visible = false;
                    txtFHno.Visible = false;
                    lbFHno.Visible = false;
                    txtFHnoCompany.Visible = false;
                    lbFHnoCompany.Visible = false;
                }
                else if (model.OrderState == 1)
                {
                    //发货单号
                    FH.Visible = true;
                    txtFHno.Visible = true;
                    lbFHno.Visible = false;
                    txtFHnoCompany.Visible = true;
                    lbFHnoCompany.Visible = false;
                }
                else
                {
                    //发货单号
                    FH.Visible = true;
                    txtFHno.Visible = false;
                    lbFHno.Visible = true;
                    txtFHnoCompany.Visible = false;
                    lbFHnoCompany.Visible = true;
                }

                string _ColumnName = "";
                var model_Column = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(int.Parse(model.PayWay));
                if (model_Column != null)
                {
                    _ColumnName = model_Column.ColumnName;
                }
                lbPayWay.Text = _ColumnName;
                lbSupportValuePrice.Text = model.SupportValuePrice.ToString();
                lbPayFactorage.Text = model.PayFactorage.ToString();
                lbBilTaxes.Text = model.BilTaxes.ToString();
                lbSalesPromotionAmount.Text = model.SalesPromotionAmount.ToString();
                txtDiscountOrder.Text = model.DiscountOrder.ToString();
                txtText.Text = model.Text;
                if (model.IsNeedBill == 1)
                {
                    lbIsNeedBill.Text = "<strong>发票抬头</strong>";
                }
            }
        }

        private void OrderProListInfo()
        {
            model = bll.GetSingle_CNVP_OrderById(id);
            if (model != null)
            {
                string html = "";
                html += "                    <table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"95%\" class=\"division\">";
                html += "                        <tr >";
                html += "                            <th style=\"text-align:center;width:36px;\">";
                html += "                                序号</th>";
                html += "                            <th style=\"text-align:center;width:100px;\">";
                html += "                                货号</th>";
                html += "                            <th style=\"text-align:center;width:200px;\">";
                html += "                                商品名称</th>";
                html += "                            <th style=\"text-align:center;width:36px;\">";
                html += "                                单价</th>";
                html += "                            <th style=\"text-align:center;width:36px;\">";
                html += "                                数量</th>";
                html += "                            <th style=\"text-align:center;width:36px;\">";
                html += "                                小计</th>";
                html += "                        </tr>";
                string Addtr = "";
                var OrderProducts_List = new BLL.CNVP_OrderProducts().GetAll_CNVP_OrderProducts(id);
                for (int i = 0; i < OrderProducts_List.Count; i++)
                {
                    Addtr += "                        <tr>";
                    Addtr += "                            <td align=\"center\">";
                    Addtr += "                                " + (i + 1) + "";
                    Addtr += "                                  <input id=\"Num\" type=\"hidden\" name=\"Num\" value=\"" + OrderProducts_List[i].Num + "\" />";
                    Addtr += "                                  <input id=\"ProductsID\" type=\"hidden\" name=\"ProductsID\" value=\"" + OrderProducts_List[i].ProductID + "\" />";//产品主表ID
                    Addtr += "                                  <input id=\"ProductsDetailID\" type=\"hidden\" name=\"ProductsDetailID\" value=\"" + OrderProducts_List[i].ProductsDetailID + "\" />";//详细表ID
                    Addtr += "                              </td>";
                    Addtr += "                            <td>";
                    Addtr += "                                " + OrderProducts_List[i].ProductNo + "</td>";
                    Addtr += "                            <td>";
                    Addtr += "                  <a href=\"/ProductDetail_" + OrderProducts_List[i].ProductClassID + "_" + OrderProducts_List[i].ProductID + ".aspx\" target=\"_blank\" title=\"" + OrderProducts_List[i].ProductName + "\">";
                    Addtr += "                                <img align=\"absMiddle\" height=\"60px\" width=\"60px\" class=\"BuyIsPrintPic\" ";
                    Addtr += "                                    src=\"/UploadFile/Product/" + OrderProducts_List[i].ImgProLoad + "\" />";
                    Addtr += "                  </a>";
                    Addtr += "                      <a href=\"/ProductDetail_" + OrderProducts_List[i].ProductClassID + "_" + OrderProducts_List[i].ProductID + ".aspx\" target=\"_blank\" title=\"" + OrderProducts_List[i].ProductName + "\">";
                    Addtr += "                                " + OrderProducts_List[i].ProductName + "";
                    Addtr += "                       </a>";
                    Addtr += "                            </td>";
                    Addtr += "                            <td>";
                    Addtr += "                                ￥" + OrderProducts_List[i].Price + "元</td>";
                    Addtr += "                            <td>";
                    Addtr += "                                " + OrderProducts_List[i].Num + "</td>";
                    Addtr += "                            <td>";
                    Addtr += "                                ￥" + OrderProducts_List[i].TotalPrice + "元</td>";
                    Addtr += "                        </tr>";
                }
                html += Addtr;
                html += "                        <tr class=\"table_data_title\">";
                html += "                            <td colspan=\"6\">";
                html += "                                共计：<font color=\"red\">" + model.ProductsTotalAmount + "</font>￥</td>";
                html += "                        </tr>";
                html += "                    </table>";
                lbOrderProList.Text = html;
            }
        }

        private void OrderStateInfo(byte ozt)
        {
            switch (ozt)
            {
                case 0:
                    us.Items.Insert(0, new ListItem("等待付款", "0"));
                    us.Items.Insert(1, new ListItem("准备发货(到付或已付)", "1"));
                    us.Items.Insert(2, new ListItem("取消", "9"));
                    break;
                case 1:
                    us.Items.Insert(0, new ListItem("配送中", "2"));
                    us.Items.Insert(1, new ListItem("取消", "9"));
                    break;
                case 2:
                    us.Items.Insert(0, new ListItem("确认收货", "4"));
                    us.Items.Insert(1, new ListItem("付款成功", "5"));
                    us.Items.Insert(2, new ListItem("退货单", "8"));
                    break;
                case 4:
                    us.Items.Insert(0, new ListItem("付款成功", "5"));
                    us.Items.Insert(1, new ListItem("已结算", "7"));
                    us.Items.Insert(2, new ListItem("退货单", "8"));
                    break;
                case 5:
                    us.Items.Insert(0, new ListItem("已结算", "7"));
                    us.Items.Insert(1, new ListItem("退货单", "8"));
                    break;
                case 7:
                    us.Items.Insert(0, new ListItem("已结算", "7"));
                    btUp.Enabled = false;//注意
                    break;
                case 8:
                    us.Items.Insert(0, new ListItem("退货单", "8"));
                    btUp.Enabled = false;//注意
                    break;
                case 9:
                    us.Items.Insert(0, new ListItem("取消", "9"));
                    btUp.Enabled = false;//注意
                    break;
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            model.ID = id;
            model.DeliveryPrice = decimal.Parse(txtDeliveryPrice.Text);
            model.DiscountOrder = decimal.Parse(txtDiscountOrder.Text);
            decimal AllPrice = 0;
            AllPrice = decimal.Parse(Labprice.Text) + decimal.Parse(txtDeliveryPrice.Text) + decimal.Parse(lbSupportValuePrice.Text) + decimal.Parse(txtDiscountOrder.Text) + decimal.Parse(lbPayFactorage.Text) + decimal.Parse(lbBilTaxes.Text) - decimal.Parse(lbSalesPromotionAmount.Text);
            model.OrderTotalAmount = AllPrice;
            model.OrderState = byte.Parse(us.SelectedValue);
            model.Text = txtText.Text;
            string ProductsID = Request.Form["ProductsID"];
            string ProductsDetailID=Request.Form["ProductsDetailID"];
            string Num = Request.Form["Num"];
            if (us.SelectedValue == "2")//配送中-库存
            {
                #region -库存
                if (!string.IsNullOrEmpty(ProductsID) && !string.IsNullOrEmpty(ProductsDetailID) && !string.IsNullOrEmpty(Num))
                {
                    string[] ProIDArray = ProductsID.Split(Convert.ToChar(","));
                    for (int i = 0; i < ProIDArray.Length; i++)//注意从‘0’开始
                    {
                        string[] ProDetailIDArray = ProductsDetailID.Split(Convert.ToChar(","));
                        string[] NumArray = Num.Split(Convert.ToChar(","));

                        int ProductsID_Int = Convert.ToInt32(ProIDArray[i]);
                        int ProductsDetailID_Int = Convert.ToInt32(ProDetailIDArray[i]);
                        int Num_Int = Convert.ToInt32(NumArray[i]);

                        if (ProductsDetailID_Int != 0)
                        {
                            var model_ProductsDetail = bll_ProductsDetail.GetSingle_CNVP_ProductsDetailById(ProductsDetailID_Int);
                            if (model_ProductsDetail != null)
                            {
                                if (Num_Int > model_ProductsDetail.StockNum)
                                {
                                    BasePage.MessageBoxGoHistory("" + model_ProductsDetail.ProDetailNo + "  库存量不足！");
                                    return;
                                }
                                
                            }
                            bll_Products.UpdateNum(ProductsID_Int, Num_Int, 0);
                            bll_ProductsDetail.UpdateNum(ProductsDetailID_Int, Num_Int, 0);
                        }
                        else
                        {
                            var model_Products = bll_Products.GetSingle_CNVP_ProductsById(ProductsID_Int);
                            if (model_Products != null)
                            {
                                if (Num_Int > model_Products.StockTotalNum)
                                {
                                    BasePage.MessageBoxGoHistory("" + model_Products.ProductNo + "  库存量不足！");
                                    return;
                                }
                            }
                            bll_Products.UpdateNum(ProductsID_Int, Num_Int, 0);
                        }
                        model.DeliveryNo = txtFHno.Text;
                        model.DeliveryCompany = txtFHnoCompany.Text;
                        bll.Update_CNVP_OrderShow3(model);
                    }
                }
                #endregion
            }
            else if (us.SelectedValue == "8" && ViewState["OrderState"].ToString() != "8")//退货
            {
                #region +库存
                if (!string.IsNullOrEmpty(ProductsID) && !string.IsNullOrEmpty(ProductsDetailID) && !string.IsNullOrEmpty(Num))
                {
                   bll.Update_CNVP_OrderShow(model);
                   string[] ProIDArray = ProductsID.Split(Convert.ToChar(","));
                   for (int i = 0; i < ProIDArray.Length; i++)//注意从‘0’开始
                   {
                       string[] ProDetailIDArray = ProductsDetailID.Split(Convert.ToChar(","));
                       string[] NumArray = Num.Split(Convert.ToChar(","));

                       int ProductsID_Int = Convert.ToInt32(ProIDArray[i]);
                       int ProductsDetailID_Int = Convert.ToInt32(ProDetailIDArray[i]);
                       int Num_Int = Convert.ToInt32(NumArray[i]);

                       if (ProductsDetailID_Int != 0)
                       {
                           bll_Products.UpdateNum(ProductsID_Int, Num_Int, 1);
                           bll_ProductsDetail.UpdateNum(ProductsDetailID_Int, Num_Int, 1);
                       }
                       else
                       {
                           bll_Products.UpdateNum(ProductsID_Int, Num_Int, 1);
                       }
                   }
                }
                #endregion
            }
            else if (us.SelectedValue == "9" && ViewState["OrderState"].ToString() != "9" && ViewState["OrderState"].ToString() != "0" && ViewState["OrderState"].ToString() != "1")//取消+库存
            {
                #region +库存
                if (!string.IsNullOrEmpty(ProductsID) && !string.IsNullOrEmpty(ProductsDetailID) && !string.IsNullOrEmpty(Num))
                {
                    bll.Update_CNVP_OrderShow(model);
                    string[] ProIDArray = ProductsID.Split(Convert.ToChar(","));
                    for (int i = 0; i < ProIDArray.Length; i++)//注意从‘0’开始
                    {
                        string[] ProDetailIDArray = ProductsDetailID.Split(Convert.ToChar(","));
                        string[] NumArray = Num.Split(Convert.ToChar(","));

                        int ProductsID_Int = Convert.ToInt32(ProIDArray[i]);
                        int ProductsDetailID_Int = Convert.ToInt32(ProDetailIDArray[i]);
                        int Num_Int = Convert.ToInt32(NumArray[i]);
                        if (ProductsDetailID_Int != 0)
                        {
                            bll_Products.UpdateNum(ProductsID_Int, Num_Int, 1);
                            bll_ProductsDetail.UpdateNum(ProductsDetailID_Int, Num_Int, 1);
                        }
                        else
                        {
                            bll_Products.UpdateNum(ProductsID_Int, Num_Int, 1);
                        }
                    }
                } 
                #endregion
            }
            else
            {
                bll.Update_CNVP_OrderShow(model);
            }
            //操作积分
            if (us.SelectedValue == "5")//付款成功
            {
                #region +操作积分
                new BLL.CNVP_Users().UpdatePointsNumType(Convert.ToInt32(ViewState["BuyerID"]), Convert.ToInt32(AllPrice), 1);//操作积分
                //再添加到记录中
                new BLL.CNVP_PointsUDRcord().Add_CNVP_PointsUDRcord(
                new Model.CNVP_PointsUDRcord()
                {
                    OperateTime = DateTime.Now.AddSeconds(1),
                    OperateDetail = "付款成功",
                    PointsNum = Convert.ToInt32(AllPrice),
                    UserID = Convert.ToInt32(ViewState["BuyerID"])
                });
                #endregion
            }
            else if (us.SelectedValue == "8" && ViewState["OrderState"].ToString() == "5")//付款成功后，退货
            {
                #region -操作积分
                new BLL.CNVP_Users().UpdatePointsNumType(Convert.ToInt32(ViewState["BuyerID"]), Convert.ToInt32(AllPrice), 0);//操作积分
                //再添加到记录中
                new BLL.CNVP_PointsUDRcord().Add_CNVP_PointsUDRcord(
                new Model.CNVP_PointsUDRcord()
                {
                    OperateTime = DateTime.Now.AddSeconds(1),
                    OperateDetail = "退货",
                    PointsNum = -Convert.ToInt32(AllPrice),
                    UserID = Convert.ToInt32(ViewState["BuyerID"])
                });
                #endregion
            }
            BasePage.MessageBox("修改成功", "OrderList.aspx?olbid=" + Request.QueryString["olbid"] + "&Page=" + Request.QueryString["Page"] + "");
        }

    }
}
